#!/usr/bin/python
# -*-coding:UTF-8 -*-

# ========================
# @Time  : 2020/6/1
# @Author: Sunny
# ========================

import re
import json
import os

from common.yaml_packaging import YamlHandle
from common.path_constants import OPINION_YAML, OPINION_SQL_CONF,OPINION_PATTERN_CONF
from common.mysql_packaging import MysqlHandle
from common.config_packaging import ConfigHandle
from parameterize.user_param import UserContextHandle
from common.context_packaging import ContextHandle
from data.com_function import time_after,generate_random_no,select_random_item,day_after

class OpinionContextHandle(ContextHandle):
    """
    定义一个实现登录账户信息的参数化
    """
    sql_config = ConfigHandle(OPINION_SQL_CONF)
    opinion_re_config = ConfigHandle(OPINION_PATTERN_CONF)

    opinion_type_id_pattern = re.compile(r'\$\{opinion_type_id\}')  # 已存在的意见箱分类id正则匹配
    no_exist_opinion_type_id_pattern = re.compile(r"\$\{no_exist_opinion_type_id\}") # 不存在的意见箱分类id正则匹配

    @classmethod
    def rep_opinion_type_id(cls,data):
        """
        替换建议箱分类id信息
        :param data:
        :return:
        """
        cls.mysql_obj = MysqlHandle()
        if re.search(cls.opinion_type_id_pattern, data):
            opinion_type_id = cls.mysql_obj(sql=cls.sql_config("opinion","opinion_type_id")).get("id")
            data = re.sub(cls.opinion_type_id_pattern, str(opinion_type_id), data)
        cls.mysql_obj.close_sql()
        return data

    @classmethod
    def edit_opinion_info_parameterize(cls, data):
        """
        编辑意见箱分类管理参数化
        :param data:
        :return:  返回data为字典类型
        """
        if isinstance(data, dict):
            data = json.dumps(data)
        data = UserContextHandle.bessky_user_parameterize(data)
        data = cls.rep_opinion_type_id(data)
        data = json.loads(data,encoding='utf-8')
        return data

    @classmethod
    def rep_used_type_id(cls,data):
        """
        替换已经有被使用过的意见分类id
        :return:
        """
        cls.mysql_obj = MysqlHandle()
        used_type_id_pattern = re.compile(eval(cls.opinion_re_config("opinion", "used_type_id")))
        if re.search(used_type_id_pattern, data):
            used_type_id = cls.mysql_obj(sql=cls.sql_config("opinion","used_type_id")).get("type")
            data = re.sub(used_type_id_pattern, str(used_type_id), data)
        cls.mysql_obj.close_sql()
        return data

    @classmethod
    def edit_opinion_type_fail_info_parameterize(cls,data):
        """
        编辑意见箱分类管理失败信息参数化
        :param data:
        :return:  返回data为字典类型
        """
        if isinstance(data, dict):
            data = json.dumps(data)
        data = UserContextHandle.bessky_user_parameterize(data)
        data = cls.rep_used_type_id(data)
        return data


    @classmethod
    def rep_no_exist_opinion_type_id(cls, data):
        """
        替换不存在的建议箱分类id信息
        :param data:
        :return:
        """
        cls.mysql_obj = MysqlHandle()
        if re.search(cls.no_exist_opinion_type_id_pattern, data):
            no_exist_opinion_type_id = int(cls.mysql_obj(sql=cls.sql_config("opinion", "no_exist_opinion_type_id")).get("id"))+1
            data = re.sub(cls.no_exist_opinion_type_id_pattern, str(no_exist_opinion_type_id), data)
        cls.mysql_obj.close_sql()
        return data

    @classmethod
    def delete_opinion_info_parameterize(cls, data):
        """
        删除建议箱分类管理参数化
        :param data:
        :return:  返回data为字典类型
        """
        if isinstance(data, dict):
            data = json.dumps(data)
        data = UserContextHandle.bessky_user_parameterize(data)
        data = cls.rep_opinion_type_id(data)
        data = cls.rep_no_exist_opinion_type_id(data)
        data = cls.rep_used_type_id(data)
        data = json.loads(data,encoding='utf-8')
        return data

    @classmethod
    def rep_opinion_id(cls,data):
        """
        建议箱历史列表页面编号和标题替换
        :param data:
        :return:
        """
        cls.mysql_obj = MysqlHandle()
        opinion_id_pattern = re.compile(eval(cls.opinion_re_config("opinion","opinion_id_pattern")))
        opinion_title_pattern = re.compile(eval(cls.opinion_re_config("opinion","title")))
        data_dict = cls.mysql_obj(sql=cls.sql_config("opinion", "id_and_title"))
        if re.search(opinion_id_pattern,data):
            opinion_id = data_dict["opinion_id"]
            data = re.sub(opinion_id_pattern,str(opinion_id),data)
        if re.search(opinion_title_pattern,data):
            title =data_dict["title"]
            data = re.sub(opinion_title_pattern, str(title), data)
        cls.mysql_obj.close_sql()
        return data

    @classmethod
    def history_list_search_info_parameterize(cls,data):
        """
        建议箱历史列表页面编号和标题信息参数化
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
            data = UserContextHandle.bessky_user_parameterize(data)
            data = cls.rep_opinion_id(data)
            data = json.loads(data)
            return data


    @classmethod
    def rep_opinion_type(cls,data):
        """
        建议箱历史别表页面状态、问题类型、申请日期、满意度、申请人工号参数化
        :param data:
        :return:
        """
        cls.mysql_obj = MysqlHandle()
        jobnumber_pattern = re.compile(eval(cls.opinion_re_config("opinion", "jobnumber")))
        type_pattern = re.compile(eval(cls.opinion_re_config("opinion","type")))
        positionId_pattern = re.compile(eval(cls.opinion_re_config("opinion","positionId")))
        startDate_pattern = re.compile(eval(cls.opinion_re_config("opinion","startDate")))
        endDate_pattern = re.compile(eval(cls.opinion_re_config("opinion","endDate")))
        satisfaction_pattern = re.compile(eval(cls.opinion_re_config("opinion","satisfaction")))
        data_dict = cls.mysql_obj(sql=cls.sql_config("opinion", "search_data"))
        jobnumber = cls.mysql_obj(sql=cls.sql_config("opinion","jobnumber"),args=(data_dict["created_dy"],))
        apply_data = data_dict["date(creation_date)"]
        if re.search(jobnumber_pattern,data):
            jobnumber = jobnumber["user_name"]
            data = re.sub(jobnumber_pattern,str(jobnumber),data)
        if re.search(type_pattern,data):
            type = data_dict["type"]
            data = re.sub(type_pattern,str(type),data)
        if re.search(positionId_pattern,data):
            positionId = data_dict["position_id"]
            data = re.sub(positionId_pattern,str(positionId),data)
        if re.search(startDate_pattern,data):
            startdate = time_after(apply_data,-30)
            data = re.sub(startDate_pattern,str(startdate),data)
        if re.search(endDate_pattern,data):
            enddate = time_after(apply_data,60)
            data = re.sub(endDate_pattern,str(enddate),data)
        if re.search(satisfaction_pattern,data):
            satisfaction = data_dict["satisfaction"]
            data = re.sub(satisfaction_pattern,str(satisfaction),data)
        cls.mysql_obj.close_sql()
        return data


    @classmethod
    def history_list_other_search_info_parameterize(cls,data):
        """
        建议箱历史列表页面搜索栏信息参数化
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
            data = UserContextHandle.bessky_user_parameterize(data)
            data = cls.rep_opinion_type(data)
            data = json.loads(data)
            return data

    @classmethod
    def rep_creat_title_info(cls,data):
        """
        替换新增建议时的标题信息
        :param data:
        :return:
        """
        creat_title_pattern = re.compile(eval(cls.opinion_re_config("opinion", "creat_title")))
        if re.search(creat_title_pattern,data):
            title = "财务系统优化" + generate_random_no(10)
            data = re.sub(creat_title_pattern,str(title),data)
        return data

    @classmethod
    def creat_suggestion_info_parameterize(cls,data):
        """
        新增建议Yaml数据信息参数化
        :param data:
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
        data = UserContextHandle.emp_user_info_parameterize(data)
        data = cls.rep_creat_title_info(data)
        return data

    @classmethod
    def rep_opinion_id_info(cls,data):
        """
        替换新增意见的id信息
        :param data:
        :return:
        """
        id_pattern = re.compile(eval(cls.opinion_re_config("opinion", "id")))
        if re.search(id_pattern,data):
            id =str(getattr(cls,"opinion_id"))
            data = re.sub(id_pattern,id,data)
        return data

    @classmethod
    def rep_state_info(cls,data,data_list):
        """
        替换state的信息
        :param data:
        :return:
        """
        state_pattern = re.compile(eval(cls.opinion_re_config("opinion", "state")))
        if re.search(state_pattern,data):
            state = select_random_item(data_list)
            data = re.sub(state_pattern,state,data)
        return data

    @classmethod
    def department_approval_info_parameterize(cls,data,data_list):
        """
        审批流信息数据参数化
        :param data:
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
        data = cls.creat_suggestion_info_parameterize(data)
        data = UserContextHandle.emp_user_info_parameterize(data)
        data = UserContextHandle.bita_supervisor_user_info_parameterize(data)
        data = UserContextHandle.bita_manager_user_info_parameterize(data)
        data = cls.rep_opinion_id_info(data)
        data = cls.rep_state_info(data,data_list)
        return data

    @classmethod
    def Invalid_success_info_parameterize(cls,data):
        """
        建议废弃信息参数化
        :param data:
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
        data = cls.creat_suggestion_info_parameterize(data)
        data = cls.rep_opinion_id_info(data)
        return data

    @classmethod
    def rep_approved_id_info(cls,data):
        """
        替换状态为已审批过的意见的id
        :param data:
        :return:
        """
        cls.mysql_obj = MysqlHandle()
        invalid_id_pattern = re.compile(eval(cls.opinion_re_config("opinion", "approved_id")))
        if re.search(invalid_id_pattern,data):
            approved_id = cls.mysql_obj(sql=cls.sql_config("opinion","approved_id"),args=("008770",(2,5,6,7,9))).get("opinion_id")
            data = re.sub(invalid_id_pattern,str(approved_id),data)
        cls.mysql_obj.close_sql()
        return data


    @classmethod
    def invalid_failure_info_parameterize(cls,data):
        """
        废弃意见失败信息参数化
        详情回复信息参数化(可共用)
        :param data:
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
        data = UserContextHandle.emp_user_info_parameterize(data)
        data = cls.rep_approved_id_info(data)
        return data


    @classmethod
    def details_info_parameterize(cls,data):
        """
        审批详情信息参数化信息参数化
        :param data:
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
        data = UserContextHandle.bessky_user_parameterize(data)
        data = cls.rep_approved_id_info(data)
        return data


    @classmethod
    def rep_approval_page_search_info(cls,data,data_dict):
        """
        建议箱待主管和待经理审批页面状态、问题类型、申请日期、申请人工号参数化
        :param data:
        :return:
        """
        opinion_id_pattern = re.compile(eval(cls.opinion_re_config("opinion", "opinion_id_pattern")))
        opinion_title_pattern = re.compile(eval(cls.opinion_re_config("opinion","title")))
        jobnumber_pattern = re.compile(eval(cls.opinion_re_config("opinion","jobnumber")))
        empName_pattern = re.compile(eval(cls.opinion_re_config("opinion","empName")))
        type_pattern = re.compile(eval(cls.opinion_re_config("opinion","type")))
        positionId_pattern = re.compile(eval(cls.opinion_re_config("opinion","positionId")))
        startDate_pattern = re.compile(eval(cls.opinion_re_config("opinion","startDate")))
        endDate_pattern = re.compile(eval(cls.opinion_re_config("opinion","endDate")))
        satisfaction_pattern = re.compile(eval(cls.opinion_re_config("opinion","satisfaction")))
        departmentId_pattern = re.compile(eval(cls.opinion_re_config("opinion","departmentId")))
        #data_dict = cls.mysql_obj(sql=cls.sql_config("opinion", "page_search_data"),args=(position_id,))
        apply_data = data_dict["date(t1.creation_date)"]
        if re.search(opinion_id_pattern, data):
            opinion_id = data_dict["opinion_id"]
            data = re.sub(opinion_id_pattern,str(opinion_id),data)
        if re.search(opinion_title_pattern,data):
            title =data_dict["title"]
            data = re.sub(opinion_title_pattern, str(title), data)
        if re.search(jobnumber_pattern,data):
            jobnumber = data_dict['job_number']
            data = re.sub(jobnumber_pattern, str(jobnumber), data)
        if re.search(empName_pattern,data):
            emp_name = data_dict['emp_name']
            data = re.sub(empName_pattern, str(emp_name), data)
        if re.search(type_pattern,data):
            type = data_dict["type"]
            data = re.sub(type_pattern,str(type),data)
        if re.search(positionId_pattern,data):
            positionId = data_dict["position_id"]
            data = re.sub(positionId_pattern,str(positionId),data)
        if re.search(startDate_pattern,data):
            startdate = time_after(apply_data,-30)
            data = re.sub(startDate_pattern,str(startdate),data)
        if re.search(endDate_pattern,data):
            enddate = time_after(apply_data,60)
            data = re.sub(endDate_pattern,str(enddate),data)
        if re.search(satisfaction_pattern,data):
            satisfaction = data_dict["satisfaction"]
            data = re.sub(satisfaction_pattern,str(satisfaction),data)
        if re.search(departmentId_pattern,data):
            data = re.sub(departmentId_pattern,str(data_dict['department_level_id']),data)
        return data


    @classmethod
    def approval_page_info_parameterize(cls,data,position_id):
        """
        待审批页面搜索栏信息参数化
        :return:
        """
        cls.mysql_obj = MysqlHandle()
        data_dict = cls.mysql_obj(sql=cls.sql_config("opinion", "page_search_data"), args=(position_id,))
        if isinstance(data,dict):
            data = json.dumps(data)
            data = UserContextHandle.bessky_user_parameterize(data)
            data = cls.rep_approval_page_search_info(data,data_dict)
        cls.mysql_obj.close_sql()
        return data

    @classmethod
    def administrator_list_info_parameterize(cls,data):
        """
        管理员列表搜索栏信息参数化
        :return:
        """
        cls.mysql_obj = MysqlHandle()
        data_dict = cls.mysql_obj(sql=cls.sql_config("opinion", "administrator_list_data"))
        if isinstance(data,dict):
            data = json.dumps(data)
            data = UserContextHandle.bessky_user_parameterize(data)
            data = cls.rep_approval_page_search_info(data,data_dict)
        cls.mysql_obj.close_sql()
        return data


    @classmethod
    def rep_exist_opinion_id_info(cls,data,data_list):
        """
        替换多个已存在的建议箱id信息
        :param data:
        :return:
        """
        cls.mysql_obj = MysqlHandle()
        opinion_id_1_pattern = re.compile(eval(cls.opinion_re_config("opinion","id_1")))
        opinion_id_2_pattern = re.compile(eval(cls.opinion_re_config("opinion", "id_2")))
        if re.search(opinion_id_1_pattern,data):
            id_1 = data_list[0]['opinion_id']
            data = re.sub(opinion_id_1_pattern,str(id_1),data)
        if re.search(opinion_id_2_pattern, data):
            id_2 = data_list[1]['opinion_id']
            data = re.sub(opinion_id_2_pattern, str(id_2), data)
        return data

    @classmethod
    def export_parameterize(cls,data):
        """
        历史列表和管理员列表导出信息参数化
        :param data:
        :return:
        """
        cls.mysql_obj = MysqlHandle()
        data_list = cls.mysql_obj(sql=cls.sql_config("opinion", "opinion_id"), fetch_all=True)
        if isinstance(data,dict):
            data = json.dumps(data)
            data = UserContextHandle.bessky_user_parameterize(data)
            data = cls.rep_exist_opinion_id_info(data,data_list)
        cls.mysql_obj.close_sql()
        return data
    # @classmethod
    # def picture_info_parameterize(cls,data):
    #     picture_pattern = re.compile(eval(cls.re_config("opinion","picture")))
    #     if re.search(picture_pattern,data):
    #         file = open(os.path.join(PICTURE_DIR,'black.jpg'),'rb')
    #         data = re.sub(picture_pattern,str(file),data)
    #     return data
    #
    # @classmethod
    # def picture_upload_info_parameterize(cls,data):
    #     if isinstance(data,dict):
    #         data = json.dumps(data)
    #         data = UserContextHandle.bessky_user_parameterize(data)
    #         data = cls.picture_info_parameterize(data)
    #     return data

    @classmethod
    def assigned_info_parameterize(cls,data):
        """
        指派用例信息参数化
        :param data:
        :return:
        """
        if isinstance(data,dict):
            data = json.dumps(data)
        data = cls.creat_suggestion_info_parameterize(data)
        data = UserContextHandle.bita_manager_user_info_parameterize(data)
        data = cls.rep_opinion_id_info(data)
        return data


    @classmethod
    def question_statistics_export_parameterize(cls,data):
        """
        提问统计列表导出信息参数化
        :param data:
        :return:
        """
        cls.mysql_obj = MysqlHandle()
        data_list = cls.mysql_obj(sql=cls.sql_config("opinion", "question_statistics_id"), fetch_all=True)
        if isinstance(data,dict):
            data = json.dumps(data)
            data = UserContextHandle.bessky_user_parameterize(data)
            data = cls.rep_exist_opinion_id_info(data,data_list)
        cls.mysql_obj.close_sql()
        return data

    @classmethod
    def rep_department_info(cls,data):
        """替换部门信息"""
        cls.mysql_obj = MysqlHandle()
        department_id_pattern = re.compile(eval(cls.re_config("dept area","dept_id")))
        if re.search(department_id_pattern,data):
            dept_id = cls.mysql_obj(sql=cls.sql_config("opinion",'department_id'),args=(1,))['department_id']
            data = re.sub(department_id_pattern,str(dept_id),data)
        cls.mysql_obj.close_sql()
        return data

    @classmethod
    def rep_data_info(cls,data):
        """替换日期信息"""
        startDate_pattern = re.compile(eval(cls.opinion_re_config("opinion", "startDate")))
        endDate_pattern = re.compile(eval(cls.opinion_re_config("opinion", "endDate")))
        if re.search(startDate_pattern,data):
            data = re.sub(startDate_pattern,str(day_after(-180)),data)
        if re.search(endDate_pattern,data):
            data = re.sub(endDate_pattern,str(day_after(30)),data)
        return data

    @classmethod
    def question_statistics_search_parameterize(cls,data):
        """提问统计查询信息参数化"""
        if isinstance(data,dict):
            data = json.dumps(data)
            data = UserContextHandle.bessky_user_parameterize(data)
            data = cls.rep_department_info(data)
            data = cls.rep_data_info(data)
        return data


if __name__ == '__main__':
    yaml_obj = YamlHandle(OPINION_YAML)
    yaml_data = yaml_obj.get_data('question_statistics_search')
    data_list = ["0"]
    data = yaml_data[1]
    print(type(data),data)
    # data = json.dumps(data)
    # print(type(data), data)
    new_data = OpinionContextHandle.question_statistics_search_parameterize(data)
    print(type(new_data), new_data)
    # data_dict = json.loads(new_data)
    # print(type(data_dict['data']['query']['flowFormIdss']),data_dict['data']['query']['flowFormIdss'])


